'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-11-10 18:03:44
'''
#
# @lc app=leetcode.cn id=1249 lang=python3
#
# [1249] 移除无效的括号
#

# @lc code=start
class Solution:
    def minRemoveToMakeValid(self, s: str) -> str:
        store = []
        remove = []
        for i, v in enumerate(s):
            if v == '(':
                store.append(i)
                remove.append(i)
            elif v == ')':
                if not store:
                    remove.append(i)
                else:
                    store.pop()
                    remove.pop()
        print(remove)
        ans = ''
        before = 0
        for i in remove:
            ans += s[before:i]
            before=i+1
        ans+=s[before:]
        return ans
# @lc code=end
if __name__ == "__main__":
    solution = Solution()
    s = ")))t((u)"
    print(solution.minRemoveToMakeValid(s))
